package com.peihua.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peihua.domain.InvoiceTitle;
import com.peihua.domain.vo.InvoiceListItem;
import com.peihua.dto.SaveInvoiceRequest;
import com.peihua.mapper.InvoiceTitleMapper;
import com.peihua.service.InvoiceTitleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class InvoiceTitleServiceImpl extends ServiceImpl<InvoiceTitleMapper, InvoiceTitle> implements InvoiceTitleService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEnterprise(SaveInvoiceRequest request) {
        if (request == null || !Objects.equals("企业", request.getTitleType())) {
            return false;
        }
        if (request.getName() == null || request.getName().isBlank()) {
            return false;
        }
        if (request.getTaxNumber() == null || request.getTaxNumber().isBlank()) {
            return false;
        }
        InvoiceTitle entity = new InvoiceTitle();
        entity.setTitleType(request.getTitleType());
        entity.setName(request.getName());
        entity.setTaxNumber(request.getTaxNumber());
        return this.save(entity);
    }

    @Override
    public List<InvoiceListItem> listAll() {
        return this.lambdaQuery()
                .select(InvoiceTitle::getId, InvoiceTitle::getName)
                .list()
                .stream()
                .map(e -> new InvoiceListItem(e.getId(), e.getName()))
                .collect(Collectors.toList());
    }
}